#include <iostream>
#include <string>
#include <vector>
#include <sw/redis++/redis.h>
#include <chrono>
#include <thread>
using std::cin;
using std::cout;
using std::endl;
using std::iostream;
using std::string;
using std::vector;

using namespace sw::redis;

template <class T>
void print(T &arr)
{

    for (auto it : arr)
    {
        cout << it << endl;
    }
}
template <class T>
void printpair(T &arr)
{

    for (auto it : arr)
    {
        cout << it.first << " : " << it.second << endl;
    }
}

template <class T>
void printoptional(const T &t)
{

    for (auto const &x : t)
    {

        if (x.has_value())
        {
            cout << x.value() << endl;
        }
    }
}

void test_zadd_zrange_zcard_zrem(Redis &redis)
{

    redis.flushall();
    redis.zadd("key1", {std::make_pair("zhangsan", 99), std::make_pair("lisi", 91), std::make_pair("tianqi", 0)});
    // 这里需要带score就传递迭代器的时候传递pair对应的，否则就直接传递String类型
    vector<std::pair<string, double>> vp;
    auto bt = std::back_inserter(vp);
    redis.zrange("key1", 0, -1, bt);
    cout << "zrange:";

    printpair(vp);
    cout << "zcard: " << redis.zcard("key1") << endl;
    redis.zrem("key1", "lisi");
    cout << "zcard: " << redis.zcard("key1") << endl;
}

void test_zrank_zscore_zinter_zunion(Redis &redis)
{

    redis.flushall();
    redis.zadd("key1", {std::make_pair("zhangsan", 99), std::make_pair("lisi", 91), std::make_pair("tianqi", 101)});
    redis.zadd("key2", {std::make_pair("zhangsan", 99), std::make_pair("lisi", 91), std::make_pair("zhaosi", 0)});

  cout<<"rank: "<< redis.zrank("key1","tianqi").value()<<endl;
  cout<<"score: "<< redis.zscore("key1","tianqi").value()<<endl;
  vector<string> v({"key1","key2"});
   redis.zinterstore("des1",v.begin(),v.end());
   redis.zunionstore("des2",v.begin(),v.end());

}

int main()
{

    Redis redis("tcp://127.0.0.1:6379");
    redis.command("select", 6);
test_zrank_zscore_zinter_zunion(redis);
    return 0;
}
